﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for DataAccessLayer
/// </summary>
public class DataAccessLayer
{
	public DataAccessLayer()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public void BindGrid(GridView imgGrid)
    {
        DataSet ds = new DataSet();

        System.Data.SqlClient.SqlDataAdapter da;

        string strSQL;



        strSQL = "Select imgId,imgTitle from imgTable";

        String connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Ashwin\Source Codes\FullPageImageGallery\FullPageImageGallery\WebApplication1\App_Data\imgDB.mdf';Integrated Security=True;User Instance=True";

        da = new System.Data.SqlClient.SqlDataAdapter(strSQL, connString);

        da.Fill(ds);



        ds.Tables[0].Columns.Add("imgFile");




        foreach (DataRow item in ds.Tables[0].Rows)
        {
            item["imgFile"] = ("imgGrab.aspx?id=" + item["imgID"]);
        }



        imgGrid.DataSource = ds;

        imgGrid.DataBind();
    }
    public bool Doc2SQLServer(string title, byte[] content, int length, string strType)
    {
        try
        {
            System.Data.SqlClient.SqlConnection cnn;

            System.Data.SqlClient.SqlCommand cmd;

            System.Data.SqlClient.SqlParameter param;

            string strSQL = string.Empty;
            strSQL = "Insert Into imgTable(imgData,imgTitle,imgType,imgLength) Values(@content,@title,@type,@length)";


            string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Ashwin\Source Codes\FullPageImageGallery\FullPageImageGallery\WebApplication1\App_Data\imgDB.mdf';Integrated Security=True;User Instance=True";
            cnn = new System.Data.SqlClient.SqlConnection(connstring);
            cmd = new System.Data.SqlClient.SqlCommand(strSQL, cnn);




            param = new System.Data.SqlClient.SqlParameter("@content", System.Data.SqlDbType.Image);



            param.Value = content;

            cmd.Parameters.Add(param);



            param = new System.Data.SqlClient.SqlParameter("@title", System.Data.SqlDbType.VarChar);

            param.Value = title;

            cmd.Parameters.Add(param);



            param = new System.Data.SqlClient.SqlParameter("@type", System.Data.SqlDbType.VarChar);

            param.Value = strType;

            cmd.Parameters.Add(param);



            param = new System.Data.SqlClient.SqlParameter("@length", System.Data.SqlDbType.BigInt);

            param.Value = length;

            cmd.Parameters.Add(param);



            cnn.Open();

            cmd.ExecuteNonQuery();

            cnn.Close();

            return true;



        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
